const express = require("express");
const app = express();

const bodyParser = require('body-parser');
const { RSA_PKCS1_OAEP_PADDING } = require("constants");
app.use(bodyParser.json());

app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1')
    if (req.method == "OPTIONS") res.send(200);
    else next();
});

const USERS = [
    { id: '01', userName: 'Zimexlc', password: '123456' },
    { id: '02', userName: 'Zimezed', password: '456789' },
    { id: '03', userName: 'SheLcen', password: '123123' }
];
const PRODUCTS = [
    { id: '01', productName: '小米手环', price: '108RMB' },
    { id: '02', productName: 'iPhone12 pro max', price: '1400$' },
]


//端口
app.listen(8080, function () {
    console.log('服务器在8080端口启动!');
});
//验证登录
app.post('/xlc', function (req, resp) {
    console.log(req.body);
    var founded = false;
    for (let user of USERS) {
        if (user.userName === req.body.userName &&
            user.password === req.body.password) {
            founded = true;
            break;
        }
    }

    if (founded) {
        resp.send({ succ: true });
    } else {
        resp.send({ succ: false, msg: '没有用户！' });
    }
    resp.end();

});

// app.post("/xlcs", function (req, resp) {
//     USERS.push(req.body);
//     resp.send({ succ: true });
//     resp.end();
// })
//用户的增加
app.post('/user', function (req, resp) {
    let founded = false;
    for (let user of USERS) {
        if (user.id === req.body.id || user.userName === req.body.userName) {
            founded = true;
            break;
        }
    }
    if (founded) {
        resp.send({ succ: false, msg: '用户名或ID重复！' });
    }

    else {
        if (req.body.id && req.body.userName && req.body.password) {
            USERS.push(req.body);
            resp.send({ succ: true });
        } else {
            resp.send({ succ: false, msg: 'id用户名或密码为空！' });
        }
    }


    resp.end();
});

//修改用户
app.put('/user', function (req, resp) {
    let founded = false;
    for (let user of USERS) {
        if (user.id === req.body.id) {
            user.userName = req.body.userName;
            user.password = req.body.password;
            founded = true;
            break;
        }
    }

    if (founded) {
        resp.send({ succ: true });
    } else {
        resp.send({ succ: false, msg: '没有用户！' });
    }
    resp.end();
});

//删除用户
app.delete('/user/:id', function (req, resp) {
    let founded = false;
    let index = 0;
    for (let user of USERS) {
        if (user.id === req.params.id) {
            USERS.splice(index, 1);
            founded = true;
            break;
        }
        index++;
    }

    if (founded) {
        resp.send({ succ: true });
    } else {
        resp.send({ succ: false, msg: '没有找到用户！' });
    }
    resp.end();

});
app.get('/user', function (req, resp) {
    console.log(USERS);
    resp.send(USERS);
    resp.end();
});
//通过id查询用户
app.get('/user/:id', (req, resp) => {
    console.log(req.params.id);
    const id = req.params.id;
    for (let user of USERS) {
        if (user.id === id) {
            resp.send([user]);
            break;
        }
    }
    resp.end();
});
//通过userName查询用户
app.get('/users/:userName', (req, resp) => {
    console.log(req.params.userName);
    const userName = req.params.userName;
    for (let user of USERS) {
        if (user.userName === userName) {
            resp.send([user]);
            break;
        }
    }
    resp.end();
});





//产品增删改查<!-------------------------------------------------------------->






app.post('/product', function (req, resp) {
    let founded = false;
    for (let product of PRODUCTS) {
        if (product.id === req.body.id || product.productName === req.body.productName) {
            founded = true;
            break;
        }
    }
    if (founded) {
        resp.send({ succ: false, msg: '用户名或ID重复！' });
    }

    else {
        if (req.body.id && req.body.productName && req.body.price) {
            PRODUCTS.push(req.body);
            resp.send({ succ: true });
        } else {
            resp.send({ succ: false, msg: 'id用户名或密码为空！' });
        }
    }


    resp.end();
});

//修改用户
app.put('/product', function (req, resp) {
    let founded = false;
    for (let product of PRODUCTS) {
        if (product.id === req.body.id) {
            product.productName = req.body.productName;
            product.price = req.body.price;
            founded = true;
            break;
        }
    }

    if (founded) {
        resp.send({ succ: true });
    } else {
        resp.send({ succ: false, msg: '没有用户！' });
    }
    resp.end();
});

//删除用户
app.delete('/product/:id', function (req, resp) {
    let founded = false;
    let index = 0;
    for (let product of PRODUCTS) {
        if (product.id === req.params.id) {
            PRODUCTS.splice(index, 1);
            founded = true;
            break;
        }
        index++;
    }

    if (founded) {
        resp.send({ succ: true });
    } else {
        resp.send({ succ: false, msg: '没有找到用户！' });
    }
    resp.end();

});
app.get('/product', function (req, resp) {
    console.log(PRODUCTS);
    resp.send(PRODUCTS);
    resp.end();
});
//通过id查询用户
app.get('/product/:id', (req, resp) => {
    console.log(req.params.id);
    const id = req.params.id;
    for (let product of PRODUCTS) {
        if (product.id === id) {
            resp.send([product]);
            break;
        }
    }
    resp.end();
});
//通过productName查询用户
app.get('/products/:productName', (req, resp) => {
    console.log(req.params.productName);
    const productName = req.params.productName;
    for (let product of PRODUCTS) {
        if (product.productName === productName) {
            resp.send([product]);
            break;
        }
    }
    resp.end();
});


